package com.olocat.cjx.nsims.controller

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.olocat.cjx.nsims.dao.EntryNurseRepository
import com.olocat.cjx.nsims.dao.NurseRepository
import com.olocat.cjx.nsims.dao.OperationLogRepository
import com.olocat.cjx.nsims.dao.SchedulingRepository
import com.olocat.cjx.nsims.pojo.entry.EntryNurse
import com.olocat.cjx.nsims.pojo.info.OperationLog
import com.olocat.cjx.nsims.pojo.info.Scheduling
import com.olocat.cjx.nsims.pojo.personnel.Nurse
import com.olocat.cjx.nsims.pojo.personnel.User
import com.olocat.cjx.nsims.service.NurseService
import com.olocat.cjx.nsims.service.UserService
import com.olocat.cjx.nsims.util.Response
import org.springframework.web.bind.annotation.*
import java.sql.Timestamp
import java.util.*
import javax.annotation.Resource

@RestController
class EntryNurseController {

    @Resource
    lateinit var entryNurseRepository: EntryNurseRepository

    @Resource
    lateinit var operationLogRepository: OperationLogRepository

    @Resource
    lateinit var nurseService: NurseService

    @Resource
    lateinit var userService: UserService

    @Resource
    lateinit var schedulingRepository: SchedulingRepository
    @Resource
    lateinit var nurseRepository: NurseRepository

    @GetMapping("/api/entry/get")
    fun get(id: String?): Response {
        id?.let {
            return Response.builder(entryNurseRepository.getOne(it))
        } ?: return Response.builder(entryNurseRepository.findAll())
    }
    @PostMapping("/api/entry/add")
    fun add(@RequestBody json: String?): Response {
        val nurse = JSON.parseObject(json, EntryNurse::class.java)
       entryNurseRepository.save(nurse)
        return Response.builder(nurse)
    }

    @PostMapping("/api/nurse/entry/agree")
    fun agree(@RequestBody json: JSONObject): Response {
        val nurse = entryNurseRepository.getOne(json["id"].toString())
        val ol = OperationLog(json["type"].toString(), json["name"].toString(), 0,
                Timestamp(Date().time), json["id"].toString(), "-")
        println(ol)
        when {
            nurse.step > 4 -> {
                return Response.builder()
            }
            nurse.step == 3 -> {
                val n = nurse.toNurse()
                n.scheduling = Scheduling()
                nurseService.save(nurse.toNurse())
                val user = User()
                user.name = nurse.name
                user.telephone = nurse.phone
                user.roleId = nurse.id
                userService.saveNurseUser(user)
                nurse.step++
                nurse.state = 1
            }
            else -> {
                nurse.step++
            }
        }
        println(nurse)
        entryNurseRepository.save(nurse)
        operationLogRepository.save(ol)
        return Response.builder()
    }

    @PostMapping("/api/nurse/entry/reject")
    fun reject(@RequestBody json: JSONObject): Response {
        val nurse = entryNurseRepository.getOne(json["id"].toString())
        val ol = OperationLog(json["type"].toString(), json["name"].toString(), 1,
                Timestamp(Date().time), json["id"].toString(), "-")
        nurse.state = 2
        entryNurseRepository.save(nurse)
        operationLogRepository.save(ol)
        return Response.builder()
    }

    @DeleteMapping("/api/nurse/entry/del")
    fun del(@RequestBody json: JSONObject): Response {
        entryNurseRepository.deleteById(json["id"].toString())
        return Response.builder()
    }
}